package com.noob.susu.excel.util;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.noob.susu.excel.entity.CellEntity;
import com.noob.susu.excel.entity.ExcelEntity;
import com.noob.susu.excel.entity.SheetEntity;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 模板解析工具
 *
 * @author shupeng_liao
 * @date 2019/10/23 17:15
 **/
@Slf4j
public class ExcelTemplateResolveUtil {

    /**
     * 获取预览数据
     *
     * @param input excel文件
     * @return ExcelEntity
     */
    public static ExcelEntity getPreview(InputStream input) {
        if (null == input) {
            log.warn("文件为空");
            return new ExcelEntity();
        }

        Workbook workbook = ExcelUtils.parseExcel(input);
        if (null == workbook) {
            log.warn("解析出excel为空");
            return new ExcelEntity();
        }

        int sheetSize = workbook.getNumberOfSheets();

        List<SheetEntity> sheetEntities = Lists.newArrayList();

        for (int i = 0; i < sheetSize; i++) {

            SheetEntity sheetEntity = new SheetEntity();
            Sheet sheet = workbook.getSheetAt(i);
            if (null == sheet) {
                continue;
            }
            //只获取第一行的数据
            Row row = sheet.getRow(0);
            if (null == row) {
                continue;
            }

            List<CellEntity> cellEntities = Lists.newArrayList();

            for (int j = 0; j <= row.getLastCellNum(); j++) {
                Cell cell = row.getCell(j);
                if (null == cell) {
                    continue;
                }
                //获取字符串值
                String cellValue = cell.getStringCellValue();
                CellEntity<String> cellEntity = new CellEntity<>();
                cellEntity.setLineNum(j);
                cellEntity.setRowNum(1);
                cellEntity.setValue(cellValue);
                cellEntity.setFieldName(j + "a");
                cellEntities.add(cellEntity);
            }
            sheetEntity.setSheetNum(i);
            sheetEntity.setCellEntities(cellEntities);
            sheetEntities.add(sheetEntity);
        }
        return new ExcelEntity(sheetEntities);
    }


    public static <T> T getObject(InputStream input, ExcelEntity excelEntity, Class<T> clazz) {
        if (null == clazz) {
            log.warn("需要转的对象为空");
            return null;
        }
        //获取对象的字段
        Field[] declaredFields = clazz.getDeclaredFields();
        List<Field> fields = Arrays.asList(declaredFields);
        if (CollectionUtils.isEmpty(fields)) {
            log.warn("需要转的对象为空");
            return null;
        }

        fields.forEach(field -> {


        });

        return null;
    }


    public static Map<String, String> getFieldMap(ExcelEntity excelEntity) {
        if (null == excelEntity || CollectionUtils.isEmpty(excelEntity.getSheetEntities())) {
            log.warn("模板为空");
            return null;
        }
        List<SheetEntity> sheetEntities = excelEntity.getSheetEntities();
        HashMap<String, String> filedMap = Maps.newHashMap();
        sheetEntities.forEach(sheetEntity -> {
            List<CellEntity> cellEntities = sheetEntity.getCellEntities();
            if (CollectionUtils.isNotEmpty(cellEntities)){
                cellEntities.forEach(cellEntity -> {
                    filedMap.put(cellEntity.getFieldName(),cellEntity.getLineNum()+"");
                });
            }
        });
        return filedMap;
    }


    public static void main(String[] args) throws Exception {
        InputStream inputStream = new BufferedInputStream(new FileInputStream(new File("C:\\Users\\shupeng_liao\\Desktop\\ces.xls")));
        ExcelEntity preview = getPreview(inputStream);
        log.info(JSONObject.toJSONString(preview));
        //maintest
    }
}
